package org.altervista.cp.thinjdbc.util;

import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class LogHelper {

	public LogHelper(Class<?> c) {
		this(c, null);
	}

	public LogHelper(Class<?> c, Level debugLevel) {
		_logger = Logger.getLogger(c.getName());
		_debugLevel = debugLevel == null ? Level.FINEST : debugLevel;
	}

	public void debug(String msg, Object... params) {
		_logger.log(getLogRecord(_debugLevel, msg, null, params));
	}

	public void debug(String msg, Throwable t, Object... params) {
		_logger.log(getLogRecord(_debugLevel, msg, t, params));
	}

	public void error(String msg, Object... params) {
		_logger.log(getLogRecord(Level.SEVERE, msg, null, params));
	}

	public void error(String msg, Throwable t, Object... params) {
		_logger.log(getLogRecord(Level.SEVERE, msg, t, params));
	}

	public void info(String msg, Object... params) {
		_logger.log(getLogRecord(Level.INFO, msg, null, params));
	}

	public void info(String msg, Throwable t, Object... params) {
		_logger.log(getLogRecord(Level.INFO, msg, t, params));
	}

	public void warn(String msg, Object... params) {
		_logger.log(getLogRecord(Level.WARNING, msg, null, params));
	}

	public void warn(String msg, Throwable t, Object... params) {
		_logger.log(getLogRecord(Level.WARNING, msg, t, params));
	}

	private LogRecord getLogRecord(Level level, String msg, Throwable t,
			Object... params) {
		LogRecord lr = new LogRecord(level, msg);
		lr.setMessage("[" + Thread.currentThread().getName() + "] - "
				+ lr.getMessage());
		lr.setSourceClassName(_logger.getName());
		if (t != null) {
			lr.setThrown(t);
		}
		if (params != null && params.length != 0) {
			lr.setParameters(params);
		}
		return lr;
	}

	private Level _debugLevel;
	private Logger _logger;

}
